﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class QuestionBLL : CommonBLL<QuestionInfo, QuestionDAL>
    {
        public static IQueryable<QuestionInfo> GetList(string search, bool? isReply, bool isCommon = false,int uid=0)
        {
            var re = GetList();
            if (uid != 0)
                re = re.Where(o => o.AskerId == uid);
            if (!string.IsNullOrEmpty(search))
            {
                re = re.Where(o => o.QuestionContentInfo.DetailContent.Contains(search) || o.AnswerContentInfo.DetailContent.Contains(search));
            }
            if (isReply.HasValue)
            {
                re = re.Where(o => o.IsAnswered == isReply);
            }
            if (isCommon)
                re = re.Where(o => o.IsCommon);
            return re;
        }

        public static int GetAnsCount(int userId)
        {
            var list = GetList();
            return list.Count(o => o.AnswerId == userId);
        }

        public static int GetAskCount(int userId)
        {
            var list = GetList();
            return list.Count(o => o.AskerId == userId);
        }

        public static int GetNotReCount()
        {
            var list = GetList();
            return list.Count(o => o.AnswerId == 0);
        }
        
        public static int GetCount()
        {
            var list = GetList();
            return list.Count();
        }

        /// <summary>
        /// 获取学生在指定时间段的提问次数
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static int GetCount(int userId, DateTime startTime, DateTime endTime)
        {
            var list = GetList();
            return list.Count(o => o.AskerId == userId && o.QuestionTime >= startTime && o.QuestionTime <= endTime);
        }

        public static QuestionInfo Get(int id)
        {
            using (var dal = new QuestionDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new QuestionDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

    }
}
